package com.kuqi.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.kuqi.fragments.BookStoreFragment;
import com.kuqi.fragments.GameRecommendFragment;
import com.kuqi.fragments.HomePageFragment;
import com.kuqi.fragments.UserPageFragment;
import com.kuqi.fragments.OthersPageFragment;

public class MainTabAdapter extends FragmentPagerAdapter {

    // 内容标题
    private static final String[] TITLE = new String[] { "个人中心","我的首页", "在线书店", "在线游戏", "其他" };

    // 构造
    public MainTabAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }


    // 所有页面都在这里一次性初始化,使用时拒绝多次初始化
    private HomePageFragment homeFragment = new HomePageFragment();
    private OthersPageFragment othersFragment = new OthersPageFragment();
    private UserPageFragment userFragment = new UserPageFragment();
    private BookStoreFragment bookStoreFragment = new BookStoreFragment();
    private GameRecommendFragment gameRecommendFragment = new GameRecommendFragment();


    @NonNull
    @Override
    public Fragment getItem(int position) {
        String msg = TITLE[position];
        System.out.println("Fragment position:" + msg);
        switch (position) {
            case 0:
                return userFragment;
            case 1:
                return homeFragment;
            case 2:
                return bookStoreFragment;
            case 3:
                return gameRecommendFragment;
            case 4:
                return othersFragment;
            default:
                return null;
        }
    }


    @Override
    public int getCount() {
        //用来获取标题数量
        return TITLE.length;
    }


    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        //这一步用来获取标题内容,如果没有则标题会为空
        return TITLE[position % TITLE.length].toUpperCase();
    }
}
